<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons");
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    dataInfo: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
      data: [],
    };
  },
  watch: {
    dataInfo: {
      deep: true,
      handler(val) {
        this.coverData();
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      // this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    coverData() {
      this.data = [
        this.dataInfo.contractMoney,
        this.dataInfo.sum90,
        this.dataInfo.sum60,
        this.dataInfo.sum30,
        this.dataInfo.profit,
        this.dataInfo.commission,
      ];
      this.$nextTick(() => {
        this.initChart();
      });
    },
    formatNumber(value) {
      // 简化数值显示
      if (value >= 1e6) return (value / 1e6).toFixed(1) + "M"; // 百万级
      if (value >= 1e3) return (value / 1e3).toFixed(1) + "K"; // 千级
      return value.toString();
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions();
      this.chart.on("click", (params) => {
        // console.log("点击的柱子数据:", params);
        // alert(`类别: ${params.name}\n值: ${params.value}`);
        let progess = ""
        if(params.name === '100%项目金额'){
          progess = "5"
        } else if(params.name === '90%项目金额') {
          progess = "4"
        } else if(params.name === '60%项目金额') {
          progess = "3"
        } else if(params.name === '30%项目金额') {
          progess = "2"
        } else {
          return
        }
        this.$emit("toIndex", progess)
      });
    },
    setOptions({ expectedData, actualData } = {}) {
      const data = this.data;
      this.chart.setOption({
        title: {
          text: "业绩数据",
          left: "left",
          textStyle: {
            fontSize: 14,
          },
        },
        tooltip: {
          trigger: "item",
          formatter: (params) => {
            return `类别: ${params.name}<br>值: ${params.value}`;
          },
        },
        grid: {
          top: "10%",
          left: "0%",
          right: "0%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: [
            "成交金额",
            "90%项目金额",
            "60%项目金额",
            "30%项目金额",
            "预计利润",
            "预计提成",
            "现金流"
          ],
          axisLabel: {
            fontSize: 10,
            interval: 0,
            rotate: 30,
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            fontSize: 10,
          },
        },
        series: [
          {
            data: [10000, 10000, 10000, 10000, 10000, 1111, 2222],
            type: "bar",
            itemStyle: {
              color: "#409eff",
            },
          },
        ],
      });
    },
  },
};
</script>
